/**
 * 
 */
package com.dalonedrau.jogl.opengl.ui;

import java.awt.Dimension;

import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.GlobalConsts;
import com.dalonedrau.jogl.engine.Interface;
import com.dalonedrau.jogl.engine.JOGLErrorHandler;
import com.dalonedrau.jogl.engine.ProjectConstants;
import com.dalonedrau.jogl.opengl.ui.border.GuiBorder;
import com.dalonedrau.vectors.Vector3;

/**
 * @author Donald
 */
public final class ToolTip extends GuiTextItemImpl {
	/** the one and only instance of the <code>ToolTip</code> class. */
	private static ToolTip	instance;
	/**
	 * the maximum width of a tooltip. private static final int
	 * MAX_TOOLTIP_WIDTH =
	 * Controller.getInstance().getGlobalInt("MAX_TOOLTIP_WIDTH");
	 */
	/**
	 * Gives access to the singleton instance of {@link ToolTip}.
	 * @return {@link ToolTip}
	 */
	public static ToolTip getInstance() {
		if (ToolTip.instance == null) {
			ToolTip.instance = new ToolTip();
		}
		return ToolTip.instance;
	}
	/** the parent the tooltip is centered upon. */
	private GuiComponent	parent;
	/** Creates a new instance of ToolTip.java. */
	private ToolTip() {
		super(ProjectConstants.getInstance().getFontH1(), "");
		super.setColor(ProjectConstants.getInstance().getTooltipFontColor());
		super.setPreferredWidth(GlobalConsts.MAX_TOOLTIP_WIDTH);
		try {
			Class c = Class.forName(
					ProjectConstants.getInstance().getTooltipBorderClass());
			super.setBorder((GuiBorder) c.getConstructors()[0].newInstance());
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	public int getParentId() {
		return parent.getId();
	}
	/**
	 * Starts the display.
	 * @param obj the tooltip parent
	 */
	public void start(final GuiComponent obj) {
		super.setPreferredSize(0, 0);
		super.setPosition(-999, -999, -999);
		//super.setPreferredWidth(GlobalConsts.MAX_TOOLTIP_WIDTH);
		super.setMaximumWidth(GlobalConsts.MAX_TOOLTIP_WIDTH);
		parent = obj;
		super.setText(parent.getTooltipText());
		Dimension d = getPreferredSize();
		Dimension p = parent.getPreferredSizeWhenDisplayingTooltip();
		// figure out the best position
		Vector3 parentPos = parent.getPreferredTooltipPosition();
		if (parentPos != null) { // should never be null
			Vector3 newPos = new Vector3(parentPos);
			newPos.move(0, -d.height, 100);
			if (newPos.getX() + d.width < GlobalConsts.SCREEN_WIDTH) {
				// will fit left-right. will it fit up-down?
				if (newPos.getY() - d.height > 0) {
					super.setPosition(
							newPos.getX(),
							newPos.getY(),
							newPos.getZ() + 999
							);
				} else {
					newPos.move(0, d.height + p.height, 0);
					super.setPosition(
							newPos.getX(),
							newPos.getY(),
							newPos.getZ() + 999
							);
				}
			} else {
				// won't fit left-right
				newPos.move(-d.width + p.width, 0, 0);
				// will fit left-right. will it fit up-down?
				if (newPos.getY() - d.height > 0) {
					super.setPosition(
							newPos.getX(),
							newPos.getY(),
							newPos.getZ() + 999
							);
				} else {
					newPos.move(0, d.height + p.height, 0);
					super.setPosition(
							newPos.getX(),
							newPos.getY(),
							newPos.getZ() + 999
							);
				}
			}
		}
		super.setVisible(true);
		Interface.getInstance().assignViewFlag(GlobalConsts.VW_02_TOOLTIP);
	}
	/** Stops the display. */
	public void stop() {
		parent = null;
		super.setText("");
		super.setVisible(false);
		Interface.getInstance().removeViewFlag(GlobalConsts.VW_02_TOOLTIP);
	}
}
